//
//  KFDMyEventsViewController.m
//  
//
//  Created by JIRENTIANXIANG on 6/24/15.
//
//

#import "KFDMyActivitiesViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDMyActivitiesViewCellComposer.h"
#import "KFDMyActivitiesViewModel.h"
#import "KFDPreferentialActivityViewController.h"
#import "ui_config.h"

@interface KFDMyActivitiesViewController ()

@property (strong,nonatomic) KFDMyActivitiesViewModel* viewModel;

@end

@implementation KFDMyActivitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"我的活动";
    UIView* header=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    header.backgroundColor=KFD_BG_COLOR;
    self.tableView.tableHeaderView=header;
    UIView* footer=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    footer.backgroundColor=KFD_BG_COLOR;
    self.tableView.tableFooterView=footer;
    self.tableView.backgroundColor=KFD_BG_COLOR;
    [self addObserver];
    self.viewModel=[[KFDMyActivitiesViewModel alloc] init];
    [self.viewModel loadPreferentialActivitiesWithMaxAccount:20 page:1];
}

-(void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activitiesReady) name:KFDMyPreferentialActivitiesReadyNotification object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KFDMyPreferentialActivitiesReadyNotification object:nil];
}

-(void)activitiesReady
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//---------------------------------------------------------------------------------------------
#pragma mark - Table view data source
//---------------------------------------------------------------------------------------------
-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.activities.count;
}

-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    UITableViewCell* cell=[[UITableViewCell alloc] init];
    KFDMyActivitiesViewCellComposer* composer=[[KFDMyActivitiesViewCellComposer alloc] init];
    composer.activity=self.viewModel.activities[indexPath.row];
    [composer layout];
    [cell.contentView addSubview:composer.view];
    return cell;
}

-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    KFDMyActivitiesViewCellComposer* composer=[[KFDMyActivitiesViewCellComposer alloc] init];
    composer.activity=self.viewModel.activities[indexPath.row];
    return [composer layout];
}
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
#pragma  mark - Table view delegate
//---------------------------------------------------------------------------------------------
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDPreferentialActivityViewController* activityVC=[[KFDPreferentialActivityViewController alloc] init];
    activityVC.url=((KFDPreferentialActivity*)self.viewModel.activities[indexPath.row]).url;
    [self.navigationController pushViewController:activityVC animated:YES];
}
//---------------------------------------------------------------------------------------------

@end
